#include "aerial\Core\Acc.h"

#include "aerial\Object\Object.h"

using namespace air;

Acc::Acc() {
	head = new IObject();
	tail = new IObject();

	head->acc = this; head->next = tail;
	tail->acc = this; tail->prev = head;
}

Acc::~Acc() {  
	clear();
	delete head;
	delete tail;
}

void Acc::clear()
{
	IObject *p = head->next, *q;
	while (p != tail)
	{
		q = p->next;
		delete p;
		p = q;
	}
	head->next = tail;
	tail->prev = head;
	count = 0;
}

bool Acc::Update(float dt)
{
	IObject *p = head->next, *q;
	while (p != tail)
	{
		if (p->update(dt)) {
			q = p->next;
			removeObject(p);
			p = q;
		}
		else p = p->next;
	}
	return false;
}

void Acc::Render()
{
	for (IObject *p = head->next; p != tail; p = p->next){
		p->render();
	}
}

void Acc::addObject(IObject* iObj)
{
	iObj->prev = tail->prev;
	iObj->next = tail;

	tail->prev->next = iObj;
	tail->prev = iObj;

	iObj->acc = this;
	++count;
}

void Acc::removeObject(IObject* iObj)
{
	iObj->prev->next = iObj->next;
	iObj->next->prev = iObj->prev;
	delete iObj;
	--count;
}

